<?php

/**
 * Description of signup
 *
 * @author iLyas
 */
class Signup extends CI_Controller {
    
    function __construct() {
        parent::__construct();
        //check if user is logged in
        //$this->is_logged_in();
    }
    
    function index(){
        $this->new_user();
    }
    
    function new_user(){   
        //load and configure Captcha
        $this->load->helper('captcha');
        
        $this->load->view('user/signup');
    }
    
    function create_member(){
        $this->load->library('form_validation');
        
        //field name, error message, validation rules
        $this->form_validation->set_rules('user_name','Name','trim|required');
        $this->form_validation->set_rules('email_address','Email Address','trim|required|valid_email');
        $this->form_validation->set_rules('username','Username','trim|required|min_length[4]');
        $this->form_validation->set_rules('password','Password','trim|required|min_length[4]');
        $this->form_validation->set_rules('password2','Password Confirmation','trim|required|min_length[4]|matches[password]');
        
        if($this->form_validation->run() == FALSE ){
            $this->new_user();
        }else{
            $this->load->model('user_model');
            if($q = $this->user_model->create_member()){
                $this->send_email_success();
                $this->load->view('user/signup_successful');
            }else{
                $this->new_user();
            }
        }
    }
    
    function send_email_success(){
        
        $email_msg = 'Dear flane \r wa ta rah twe7echnaak a sat ! \r\r\r peace a l3ewd';
        
        $this->load->library('email');
        
        $this->email->from('Welcom@ayouda.me', 'Ilyas Bakouch');
        $this->email->to($this->input->post('email_address'));

        $this->email->subject('your account has been created');
        $this->email->message($email_msg);

        $this->email->send();

        echo $this->email->print_debugger();
    }
    function is_logged_in(){
        $is_logged_in = $this->session->userdata('is_logged_in');
        
        if(isset($is_logged_in) || $is_logged_in == TRUE){
            redirect('/user/profile/members_area', 'refresh');
        }
    }
}

?>
